<script lang="ts" setup>
import {onMounted, ref} from "vue";
import GenCodeBO from "@/domain/bo/GenCodeBO";
import {useRouter} from "vue-router";
import {ElMessage} from "element-plus";
import GlobalConstant from "@/domain/constants/GlobalConstant";
import dataSourceService from "@/service/DataSourceService";
import PageBO from "@/domain/bo/PageBO";
import DataSourceInfoVO from "@/domain/vo/DataSourceInfoVO";
import genTemplateService from "@/service/GenTemplateService";
import GenTemplateGroupVO from "@/domain/vo/GenTemplateGroupVO";
import genCodeTaskService from "@/service/GenCodeTaskService";

interface Option {
  key: number
  label: string
  disabled: boolean
}


const router = useRouter();

//========================== 生命周期 ==========================
const datSourceList = ref(new Array<DataSourceInfoVO>);
const tableNameList = ref(new Array<Option>);
const genTemplateGroupList = ref(new Array<GenTemplateGroupVO>);
const genTemplateList = ref(new Array<Option>);
const genCodeBO = ref(new GenCodeBO());
onMounted(() => {
  //加载数据
  let page = new PageBO();
  page.page = 1;
  page.pageSize = 999;
  datSourceList.value = [];
  dataSourceService.list(page)
      .then(res => {
        res.list.forEach(item => datSourceList.value.push(DataSourceInfoVO.createFormObject(item)))
      })
  genTemplateService.groupList()
      .then(res => {
        res.forEach(item => genTemplateGroupList.value.push(GenTemplateGroupVO.createFormObject(item)))
      })
})

//========================== 按钮事件 ==========================
async function subGenCodeBO() {
  let flag = false;
  try {
    let res = await genCodeTaskService.genCode(genCodeBO.value);
    flag = true;
    ElMessage.success(GlobalConstant.SAVE_SUCCESS_MSG);
  } catch (error) {
    ElMessage.error(GlobalConstant.SAVE_ERROR_MSG + error.msg);
  }
  return flag;
}

function getTableName(dataSourceId: string) {
  dataSourceService.tableList(dataSourceId)
      .then(res => {
        tableNameList.value = [];
        res.forEach(item => {
          tableNameList.value.push({
            key: item,
            label: item
          } as Option);
        })
      })
}

function getTemplateFilesName(genTemplateGroupId: string) {
  for (let i = 0; i < genTemplateGroupList.value.length; i++) {
    let item: GenTemplateGroupVO = genTemplateGroupList.value[i];
    if (item.id == genTemplateGroupId) {
      genTemplateList.value = [];
      item.fileList.forEach(j => {
        genTemplateList.value.push({
          key: j.id,
          label: j.genFileName
        } as Option);
      });
    }
  }
}

//========================== 回调事件 ==========================
async function subHandle() {
  await subGenCodeBO();
  router.back()

}

</script>
<template>
  <div>
    <el-form
        :model="genCodeBO"
        label-width="120px"
        class="demo-ruleForm"
        status-icon
    >
      <el-form-item>
        <el-button type="primary" @click="subHandle()">
          提交
        </el-button>
      </el-form-item>
      <el-form-item label="选择数据源">
        <el-select v-model="genCodeBO.dataSourceId" @change="getTableName(genCodeBO.dataSourceId)">
          <el-option
              v-for="item in datSourceList"
              :key="item.id"
              :label="item.host + ':' + item.port + '/' + item.schemaName"
              :value="item.id">
            <span style="float: left">{{ item.host + ':' + item.port + '/' + item.schemaName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.databaseTypeName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择表">
        <el-transfer
            v-model="genCodeBO.tableNames"
            :titles="['未选', '已选']"
            :data="tableNameList">
        </el-transfer>
      </el-form-item>
      <el-form-item label="选择模板">
        <el-select v-model="genCodeBO.genTemplateGroupId" @change="getTemplateFilesName(genCodeBO.genTemplateGroupId)">
          <el-option
              v-for="item in genTemplateGroupList"
              :key="item.id"
              :label="item.groupName"
              :value="item.id">
            <span style="float: left">{{ item.groupName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择模板文件">
        <el-transfer
            v-model="genCodeBO.templateFileIds"
            :titles="['未选', '已选']"
            :data="genTemplateList">
        </el-transfer>
      </el-form-item>
    </el-form>

  </div>
</template>
<style lang="scss" scoped>

</style>